<?php

/**
 * 记录POST访问到日志
 * @return void
 */
function log_post_data()
{
    $method = $_SERVER['method'];
    if ($method != 'POST') return;
    $post = $_POST;
    isset($post['password']) AND $post['password'] = '******';
    isset($post['password_new']) AND $post['password_new'] = '******';
    isset($post['password_old']) AND $post['password_old'] = '******';
    log_tofile(json_encode($post, JSON_PRETTY_PRINT), 'post_data');
}

/**
 * 记录错误到日志文件
 * @param $errno
 * @param $errstr
 * @param $errfile
 * @param $errline
 * @return bool
 */
function log_error($errno, $errstr, $errfile, $errline)
{
    $subject = "Error[$errno]: $errstr, File: $errfile, Line: $errline";
    $message = array();
    log_tofile($subject, 'error.log');
    $arr = debug_backtrace();
    array_shift($arr);
    foreach ($arr as $v) {
        $args = '';
        if (!empty($v['args']) && is_array($v['args'])) foreach ($v['args'] as $v2) $args .= ($args ? ' , ' : '') . (is_array($v2) ? 'array(' . count($v2) . ')' : (is_object($v2) ? 'object' : $v2));
        !isset($v['file']) AND $v['file'] = '';
        !isset($v['line']) AND $v['line'] = '';
        $message [] = "File: $v[file], Line: $v[line], $v[function]($args) ";
    }
    $txt = $subject . "\r\n" . implode("\r\n", $message);

    if (DEBUG) {
        $html = $s = "<fieldset class=\"fieldset small notice\">
			<b>$subject</b>
			<div>" . implode("<br>\r\n", $message) . "</div>
		</fieldset>";
        echo $html;
        log_tofile($txt, 'debug_error.log');
    }

    return true;
}

/**
 * 插入日志到数据库，并能在后台日志中访问
 * @param string $stype tag
 * @param string $content 内容
 * @param string $othermsg 其他内容，一般填写当前信息的触发页面与行数
 * @return bool
 */
function log_todb($stype, $content, $othermsg = "")
{
    $article = array();
    $article['cid'] = 14;
    $article['stype'] = $stype;
    $article['content'] = $content;
    $article['shijian'] = time();
    $article['page'] = $othermsg;
    $id = uuu_ainsert($article);
//    echo $id;
    if (is_numeric($id)) {
        return true;
    }

    return false;
}

/**
 * 自定义内容日志到自定义文件
 * @param $s
 * @param $file
 * @return void
 */
function log_tofile($s, $file = 'site.log')
{
    $time = time();
    $ip = ip();
    $day = date('Ym', $time);
    $mtime = date('Y-m-d H:i:s');
    $url = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
    $logpath = LogDir . $day;
    !is_dir($logpath) AND mkdir($logpath, 0777, true);
    if (is_array($s)) {
        $s = join("\t", $s);
    }
    $s = str_replace(array("\r\n", "\n", "\t"), ' ', $s);
    $s = "$mtime\t$ip\t$url\t$s\r\n";
    @error_log($s, 3, $logpath . "/$file");
}

/**
 * 记录到预设的info日志文件中
 * @param $s
 * @return void
 */
function log_info($s)
{
    log_tofile($s, "info.log");
}

/**
 * 记录到预设的debug日志文件中
 * @param $s
 * @return void
 */
function log_debug($s)
{
    log_tofile($s, "debug.log");
}

/**
 * 友好的输出调试信息
 * @param mixed $var
 * @param bool $die
 */
function dump_pre($var, $die = false)
{
    echo '<pre>';
    var_dump($var);
    echo '</pre>';
    if ($die) die;
}

/**
 * 计时启动，结合log_time_end函数，并记录到日志中
 * @param $TAG
 * @return void
 */
function log_time_start($TAG)
{
    setGlobals("log_time_" . $TAG, microtime_float());
}

/**
 * 计时结束，结合log_time_start函数，并记录到日志中
 * @param $TAG
 * @param $file
 * @return void
 */
function log_time_end($TAG, $file = "site.log")
{
    $s = (microtime_float() - getGlobals("log_time_" . $TAG)) * 1000;
    setGlobals("log_time_" . $TAG, null);
    log_tofile($TAG . " " . $s . " ms ", $file);
}

/**
 * 页面请求访问记录，记录到数据库，并能在后台的访问记录中查看到
 * @param $isok
 * @return void
 */
function log_access($isok = true)
{
    $info = array();
    $info['url'] = getRequestUri();
    $info['ip'] = ip();
    $info['loginname'] = qqq_backadmin_name() ? qqq_backadmin_name() : "未登录";
    $robot = is_robot();
    $info['robot'] = $robot ? "搜索引擎" : "";
    $info['referer'] = get_referer_url()?get_referer_url():"直接输入";
    $info['isok'] = $isok === true ? "访问成功" : $isok;
    $info['useragent'] = $_SERVER['HTTP_USER_AGENT'];

    qqq_add_art(20, $info);
}